#include "netserver.h"

#include <boost/bind.hpp>
#include <boost/asio.hpp>

#include <iostream>

NetServer::NetServer(const unsigned short &a_port)
    : m_service(NetService::IOService())
    , m_tcp_acceptor(m_service.getIOService(),
        boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), a_port))
    , m_room()
{
    start_accept();
}

void NetServer::start_accept()
{
    NetChatSessionPtr l_new_session(new NetChatSession(m_room));

    m_tcp_acceptor.async_accept(l_new_session->socket(),
        boost::bind(&NetServer::handle_accept, this, l_new_session,
            boost::asio::placeholders::error));
}

void NetServer::handle_accept(NetChatSessionPtr a_session,
    const boost::system::error_code& a_error)
{
    std::cout << "New session" << std::endl;
  if (!a_error)
  {
    a_session->start();
  }

  start_accept();
}

NetServer::~NetServer()
{

}
